package com.dituhui.gps.service;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.dituhui.tool.io.Constants;
import com.dituhui.tool.io.HttpTool;

public class AdjustGPS {
	private static Logger LOGGER = Logger.getLogger(AdjustGPS.class);
	// 从配置文件读取URL模板
	private static String URL = Constants.getValue("AdjustGPSURL", "gps_config.properties");

	/**
	 * 根据坐标获取定位漂移结果
	 * 
	 * @param x 经纬度X
	 * @param y 经纬度Y
	 * @return 定位漂移结果
	 * @throws Exception
	 */
	public String getGPS(double x, double y) {
		if (URL == null && URL.length() == 0) {
			// 未配置URL时，返回null
			return null;
		}

		// 复制url，避免多线程里同时修改url
		String copyUrl = new String(URL);

		StringBuffer coor = new StringBuffer();
		// coor.append("{");
		coor.append("\"x\":").append(x).append(",");
		coor.append("\"y\":").append(y);
		// coor.append("}");

		copyUrl = copyUrl.replace("#point#", coor);
		// System.out.println("copyUrl = " + copyUrl);

		String result = null;
		try {
			result = HttpTool.getContentByGET(copyUrl, null);
		} catch (IOException e) {
			LOGGER.info("getGPS发生异常。url=" + copyUrl);
		}
		return result;
	}
}
